<?php

namespace app\admin\controller;

use app\models\{Admin, OrderGoods, Shoplist};
use think\Db;
use think\Facade\Config as Configs;


class Main extends Base
{
    public function welcome()
    {
        $admin = $this->request->admin;
        $shopmodel = new Shoplist();
        $rank = (new OrderGoods())->where('status', 4)->order('id desc');
        $data = input();
        if (!empty($data['shopid'])) {
            $rank = $rank->where('shopid', $data['shopid']);
        }
        if ($admin['type'] == 2) {
            $shopid = $shopmodel->where(['area' => $admin['area']])->column('id');
            if (count($shopid) > 0) {
                $result = $rank->where(['shopid' => $shopid]);
            } else {
                $result = $rank->where(['shopid' => 0]);
            }
        }
        if ($admin['type'] == 3 || $admin['type'] == 4) {
            $shopid = $shopmodel->where(['street' => $admin['street']])->column('id');
            if (count($shopid) > 0) {
                $result = $rank->where(['shopid' => $shopid]);
            } else {
                $result = $rank->where(['shopid' => 0]);
            }
        }
        $statc = [
            'goods' => [],
            'goodstotal' => [],
            'today' => [],
        ];
        $rank = $rank->select();
        $goods = [];
        $todaygoods = [];
        foreach ($rank as &$ranks) {
            $goods[$ranks['goodsid']] = $goods[$ranks['goodsid']] ?? [];
            $goods[$ranks['goodsid']]['number'] = $goods[$ranks['goodsid']]['number'] ?? 0;
            $goods[$ranks['goodsid']]['total'] = $goods[$ranks['goodsid']]['total'] ?? 0;
            $goods[$ranks['goodsid']]['goodsid'] = $ranks['goodsid'];
            $goods[$ranks['goodsid']]['title'] = $ranks['title'];
            $goods[$ranks['goodsid']]['images'] = json_decode($ranks['images'], true);
            $goods[$ranks['goodsid']]['shopid'] = $ranks['shopid'];
            $goods[$ranks['goodsid']]['number'] += $ranks['number'];
            $goods[$ranks['goodsid']]['total'] += $ranks['total'];
            if ($ranks['fintime'] >= date('Y-m-d')) {
                $todaygoods[$ranks['goodsid']] = $todaygoods[$ranks['goodsid']] ?? [];
                $todaygoods[$ranks['goodsid']]['number'] = $todaygoods[$ranks['goodsid']]['number'] ?? 0;
                $todaygoods[$ranks['goodsid']]['total'] = $todaygoods[$ranks['goodsid']]['total'] ?? 0;
                $todaygoods[$ranks['goodsid']]['goodsid'] = $ranks['goodsid'];
                $todaygoods[$ranks['goodsid']]['title'] = $ranks['title'];
                $todaygoods[$ranks['goodsid']]['images'] = json_decode($ranks['images'], true);
                $todaygoods[$ranks['goodsid']]['shopid'] = $ranks['shopid'];
                $todaygoods[$ranks['goodsid']]['number'] += $ranks['number'];
                $todaygoods[$ranks['goodsid']]['total'] += $ranks['total'];
            }
        }
        array_multisort(array_column($goods, 'number'), SORT_DESC, $goods);
        $statc['goods'] = $goods;
        array_multisort(array_column($goods, 'total'), SORT_DESC, $goods);
        $statc['goodstotal'] = $goods;
        $statc['today'] = $todaygoods;
        return view(null, [
            'admin' => $admin,
            'statc' => $statc
        ]);
    }

    public function index()
    {
        $admin = $this->request->admin;
        $menu = config('menu.menu');
        $arr = [];
        foreach ($menu as $k => $v) {
            if ($v['show'] == true) {
                foreach ($v['sub'] as $x => $y) {
                    if (($y['show'] == true && in_array($y['uri'], $admin['role'])) || $admin['is_default'] == 1) {
                        $arr[$k]['name'] = $v['name'];
                        $arr[$k]['ico'] = $v['ico'];
                        $arr[$k]['sub'][$x] = $y;
                    }
                }
            }
        }
        return view(null, [
            'admin' => $admin,
            'menu' => $arr,
        ]);
    }


    public function miss()
    {
        return view('404');
    }

    public function admin()
    {
        $admin = Admin::get(session('admin_id'));
        if ($this->request->isPost()) {
            if (empty($admin)) {
                json(['code' => 1, 'msg' => '登陆已过期!']);
            }

            $data = input('post.');
            if ($data['password'] != '') {
                if ($data['password'] != $data['passworder']) {
                    return json(['code' => 1, 'info' => '密码不一致']);
                }
                $data1['password'] = md5(md5($data['password']) . md5($admin['entry']));
            }
            $data1['nickname'] = $data['nickname'];
            if ($admin->save($data1, ['id' => $admin->id])) {
                return json(['code' => 0, 'msg' => '操作成功']);
            } else {
                return json(['code' => 1, 'msg' => '操作失败']);
            }
        } else {
            return view('admin', ['admin' => $admin]);
        }
    }
}
